package com.ttt.mapper.impl;

import com.ttt.mapper.SelectCourseInter;
import com.ttt.pojo.Course;
import com.ttt.utils.DbConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SelectCoureMapperImpl implements SelectCourseInter {

    @Override
    public List<Course> findCourse() throws SQLException {
        //调用连接数据库工具类
        DbConnectionUtil dbConnectionUtil = new DbConnectionUtil();
        Connection connection = dbConnectionUtil.getConnection();

        //sql语句
        String selectCourseSql = "select * from course";
        ResultSet resultSet = connection.createStatement().executeQuery(selectCourseSql);
        //存放课程信息
        List<Course> list = new ArrayList<>();


        while (resultSet.next()){
            Course course = new Course();
            String className = resultSet.getString("className");
            String classType = resultSet.getString("classType");
            String teacherName = resultSet.getString("teacher");
            String academy = resultSet.getString("academy");  //开课学院
            String credits = resultSet.getString("credits");
            String time = resultSet.getString("time");  //上课时段
            String address = resultSet.getString("address"); //授课地点
            String classContent = resultSet.getString("classContent");  //课程介绍

            //赋值给coure
            course.setClassName(className);
            course.setClassType(classType);
            course.setTeacherName(teacherName);
            course.setAcademy(academy);
            course.setCredits(credits);
            course.setTime(time);
            course.setAddress(address);
            course.setClassContent(classContent);

            //放在list
            list.add(course);

        }
        System.out.println("mapperList" + list);
        return list;


    }

    @Override
    public List<Course> conditionFindC(String classType, String academy, String time) throws SQLException {
        //调用连接数据库工具类
        DbConnectionUtil dbConnectionUtil = new DbConnectionUtil();
        Connection connection = dbConnectionUtil.getConnection();
        //判断查询条件进行sql语句编辑

        //情况1，三者都为空，即查询全部
        if(classType.equals("null") && academy.equals("null") && time.equals("null")){
            //调用查询全部的方法
            this.findCourse();
        }
        //情况2：三者有一空，其两不为空
        if (classType.equals("null") && !academy.isEmpty() && !time.isEmpty()){
            //通过academy time 进行条件查询

        }else if(academy.equals("null") && !classType.isEmpty() && !time.isEmpty()){

        }else if(time.equals("null") && !classType.isEmpty() && !academy.isEmpty()){
            //通过classType academy进行查询
            String sql ="select * from course where classType = ? and academy = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,classType);
            preparedStatement.setString(2,academy);
            //进行查找
            int i = preparedStatement.executeUpdate();
            System.out.println("i = " + i);

            //关闭资源
            connection.close();
            preparedStatement.close();
        }

        //情况3：两者为空，有一者不为空
        if (classType.equals("null") && academy.equals("null") && !time.isEmpty()){

        }else if (classType.equals("null") && time.equals("null") && !academy.isEmpty()){

        } else if (academy.equals("null") && time.equals("null") && !classType.isEmpty()) {

        }

        return null;
    }
}
